﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;

using PH.Model.Accounts;
using PH.Logic.Accounts;
namespace PH.Web.Global.Membership
{
    public class RoleProvider:System.Web.Security.RoleProvider
    {
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                return "PhysicalHealth";
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            RoleLogic.CreateNewRole(new RoleModel() { RoleName = roleName });
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            var users = UserLogic.FindUsersInRole(roleName, usernameToMatch);
            if (users != null && users.TotalRecordsCount > 0)
                return users.Select(p => p.UserName).ToArray();
            else
                return null;
        }

        public override string[] GetAllRoles()
        {
            var roles = RoleLogic.GetAllRoles();
            if (roles != null && roles.TotalRecordsCount > 0)
                return roles.Select(p => p.RoleName).ToArray();
            else
                return null;
        }

        public override string[] GetRolesForUser(string username)
        {
            var roles = RoleLogic.GetRolesForUser(username);
            if (roles != null && roles.TotalRecordsCount > 0)
                return roles.Select(p => p.RoleName).ToArray();
            else
                return null;
        }

        public override string[] GetUsersInRole(string roleName)
        {
            var users = UserLogic.GetUsersInRole(roleName);
            if (users != null && users.TotalRecordsCount > 0)
                return users.Select(p => p.UserName).ToArray();
            else
                return null;
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            return UserLogic.IsUserInRole(username, roleName);
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            UserLogic.RemoveUsersFromRoles(usernames, roleNames);
        }

        public override bool RoleExists(string roleName)
        {
            return RoleLogic.RoleExists(roleName);
        }

        #region 未实现的方法
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
